package com.cskaoyan.service.wx_search;

import com.cskaoyan.bean.wx_history.History;
import com.cskaoyan.bean.wx_history.HistoryExample;
import com.cskaoyan.bean.wx_search.Keyword;
import com.cskaoyan.bean.wx_search.KeywordExample;
import com.cskaoyan.bean.wx_search.vo.IndexVo;
import com.cskaoyan.mapper.wx_history.HistoryMapperByWxSearch;
import com.cskaoyan.mapper.wx_search.KeywordMapperBySearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 类<code>Doc</code>用于：TODO
 *
 * @author Acher
 * @version 1.0
 * @date 2021-10-22
 */
@Service
public class WxWxSearchServiceImpl implements WxSearchService {

    @Autowired
    KeywordMapperBySearch keywordMapper;
    @Autowired
    HistoryMapperByWxSearch historyMapperByWxSearch;

    @Override
    public IndexVo index() {
        // 先查表内所有数据，再封装到IndexVo中
        IndexVo indexVo = new IndexVo();
        List<Keyword> hotKeywordList = new ArrayList<>();
        List<Keyword> keywords = keywordMapper.selectByExample(new KeywordExample());
        for (Keyword keyword : keywords) {
            if (keyword.getIsDefault()) {// 封装默认keyword
                indexVo.setDefaultKeyword(keyword);
            }
            hotKeywordList.add(keyword);// 封装所有keyword对象到list中
        }
        HistoryExample example = new HistoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        List<History> historyList = historyMapperByWxSearch.selectByExample(example);
        indexVo.setHotKeywordList(hotKeywordList);
        indexVo.setHistoryKeywordList(historyList);
        return indexVo;
    }

    @Override
    public List<String> helper(String keyword) {
        KeywordExample example = new KeywordExample();
        example.createCriteria().andKeywordLike("%" + keyword + "%");
        List<Keyword> keywordList = keywordMapper.selectByExample(example);
        List<String> keywords = new ArrayList<>();
        keywordList.forEach(item -> keywords.add(item.getKeyword()));
        return keywords;
    }

    @Override
    public boolean clearhistory() {
        HistoryExample example = new HistoryExample();
        example.createCriteria().andDeletedEqualTo(false);
        History history = new History();
        history.setDeleted(true);
        return historyMapperByWxSearch.updateByExampleSelective(history, example) != 0;
    }
}
